package org.geek4j.iTextT;

import java.io.FileOutputStream;

import org.junit.Test;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * @author Geek4J
 * @description iTextPDF T
 */
public class TableExample {

	/**
	 * @author Geek4J
	 * @description Table T
	 */
	@Test
	public void tableT() throws Exception {
		Document document = new Document();

		PdfWriter.getInstance(document, new FileOutputStream("HelloWorld-Table.pdf"));

		document.open();

		Paragraph preface = new Paragraph();

		// add title
		Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
		Paragraph title = new Paragraph("Powered by Geek4J", catFont);
		title.setAlignment(Element.ALIGN_CENTER);

		// add empty line
		preface.add(title);
		preface.add(new Paragraph(" "));

		document.add(preface);

		// add table header
		PdfPTable header = new PdfPTable(7);

		PdfPCell cell1 = new PdfPCell(new Paragraph("Title 1"));
		PdfPCell cell2 = new PdfPCell(new Paragraph("Title 2"));
		PdfPCell cell3 = new PdfPCell(new Paragraph("Title 3"));
		PdfPCell cell4 = new PdfPCell(new Paragraph("Title 4"));
		PdfPCell cell5 = new PdfPCell(new Paragraph("Title 5"));
		PdfPCell cell6 = new PdfPCell(new Paragraph("Title 6"));
		PdfPCell cell7 = new PdfPCell(new Paragraph("Title 7"));

		header.addCell(cell1);
		header.addCell(cell2);
		header.addCell(cell3);
		header.addCell(cell4);
		header.addCell(cell5);
		header.addCell(cell6);
		header.addCell(cell7);

		document.add(header);

		// add table content
		for (int i = 0; i < 17; i++) {
			PdfPTable content = new PdfPTable(7);
			for (int j = 0; j < 7; j++) {
				PdfPCell column = new PdfPCell(new Paragraph("Data {0} * {1}".replace("{0}", i + "").replace("{1}", j + "")));
				content.addCell(column);
			}
			document.add(content);
		}
		document.close();
	}
}